package com.example.pinkcat.activity.changuserinfo;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.StrictMode;
import android.provider.MediaStore;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.example.pinkcat.R;
import com.example.pinkcat.activity.api.getData.CopyFileByUri;
import com.example.pinkcat.activity.api.getData.GetData;

import com.rishabhharit.roundedimageview.RoundedImageView;


import java.io.ByteArrayOutputStream;

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Arrays;
import java.util.Objects;


public class headimg extends AppCompatActivity {
    Context context=this;
    RoundedImageView head;
    String file_path;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Objects.requireNonNull(getSupportActionBar()).hide();//去掉标题栏
//        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
//                WindowManager.LayoutParams.FLAG_FULLSCREEN);//去掉信息栏
        setContentView(R.layout.activity_headimg);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            getWindow().getDecorView().setSystemUiVisibility( View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN| View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
        }//深色信息栏
        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
        StrictMode.setThreadPolicy(policy);//允许此线程内发起网络请求

        head=findViewById(R.id.edit_head);

        startPhoto();
        listen();
    }
    void listen(){
        //更换头像btn
        head.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startPhoto();
            }
        });
        //返回键监听
        findViewById(R.id.back).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
        //确认修改键监听
        findViewById(R.id.change_enter).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (file_path==null){
                    Toast.makeText(context,"请选择新的图像",Toast.LENGTH_SHORT).show();
                }else {
                    if (GetData.updateHead(file_path,context))
                        onBackPressed();
                }
            }
        });
    }
    void startPhoto(){
        Intent intent = new Intent(Intent.ACTION_PICK, null);
        intent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image/*");
        startActivityForResult(intent, 2);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 2) {
            // 从相册返回的数据
            //Log.e(this.getClass().getName(), "Result:" + data.toString());
            if (data != null) {
                // 得到图片的全路径
                Uri uri = data.getData();
                Glide.with(context).load(uri).into(head);
                file_path=CopyFileByUri.getPath(context,uri,"image.png");
            }
        }
    }
}